<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="6">
            <a-form-item label="供应商代码">
              <a-input placeholder="请输入供应商代码" v-model="queryParam.serial" dict="hf_supplier,name,name">
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="供应商名称">
              <j-search-select-tag
                placeholder="请输入供应商名称"
                v-model="queryParam.name"
                dict="hf_supplier,name,name"
                @change="searchQuery"
              >
              </j-search-select-tag>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
      <a-button type="primary" @click="searchReset" icon="reload">重置</a-button>
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('供应商')">导出</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button
        style="background: #FF8F00;border: #FF8F00;"
        icon="import"
        type="primary"
        @click="uploadModalJD.visible = true"
        >金蝶导入
      </a-button>
      <a-modal
        :visible="uploadModalJD.visible"
        @cancel="
          () => {
            uploadModalJD.visible = false
          }
        "
      >
        <a-spin :spinning="uploadModalJD.loading">
          <a-row :gutter="[16, 16]">
            <a-col :span="24">
              <a-upload :multiple="false" :showUploadList="false" name="file" :customRequest="customUpload">
                <a-button icon="import" type="primary">选择金蝶导出的文件</a-button>
              </a-upload>
            </a-col>
          </a-row>
        </a-spin>
      </a-modal>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down"/></a-button>
      </a-dropdown>
      <!-- 高级查询区域 -->
      <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
        v-show="false"
      ></j-super-query>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleDelete(record.id)">删除</a>
        </span>
      </a-table>
    </div>

    <hf-supplier-modal ref="modalForm" @ok="modalFormOk"></hf-supplier-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfSupplierModal from './modules/HfSupplierModal'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import store from '@/store'
import { Modal } from 'ant-design-vue'
import { postAction } from '@api/manage'

export default {
  name: 'HfSupplierList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    HfSupplierModal
  },
  data() {
    return {
      description: '供应商管理页面',
      uploadModalJD: {
        visible: false,
        loading: false
      },
      isorter: {
        column: 'serial',
        order: 'asc'
      },
      // 表头
      columns: [
        {
          title: '代码',
          align: 'center',
          dataIndex: 'serial'
        },
        {
          title: '供应商名称',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '地址',
          align: 'center',
          dataIndex: 'address'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        // {
        //   title:'附件',
        //   align:"center",
        //   dataIndex: 'fileSlot',
        //   scopedSlots: { customRender: 'fileSlot' }
        // },
        // {
        //   title:'图片',
        //   align:"center",
        //   dataIndex: 'imgSlot',
        //   scopedSlots: { customRender: 'imgSlot' }
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/purchase/hfSupplier/list',
        delete: '/purchase/hfSupplier/delete',
        deleteBatch: '/purchase/hfSupplier/deleteBatch',
        exportXlsUrl: '/purchase/hfSupplier/exportXls',
        importExcelUrl: 'purchase/hfSupplier/importExcel',
        importExcelUrlByJD: 'purchase/hfSupplier/importExcelByJD'
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    importExcelUrlByJD: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrlByJD}`
    }
  },
  methods: {
    customUpload(options) {
      this.uploadModalJD.loading = true
      const { file, onProgress, onSuccess, onError } = options
      const formData = new FormData()
      formData.append('file', file)
      postAction(this.importExcelUrlByJD, formData, { headers: this.tokenHeader, timeout: 0 })
        .then(res => {
          if (res.success) {
            this.$message.success(res.message)
            this.close()
          } else {
            this.$message.warning(res.message)
          }
        })
        .catch(err => {
          console.log('err', err)
        })
        .finally(() => {
          this.uploadModalJD.visible = false
          this.uploadModalJD.loading = false
        })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
